<template>
  <view :style="{marginTop:marginTop+'rpx',marginBottom:marginBottom+'rpx'}" class="tui-slide-view">
    <view :change:disable="handler.disableChange" :change:duration="handler.durationChange"
          :change:prop="handler.sizeReady"
          :change:rebounce="handler.rebounceChange" :change:show="handler.showChange" :disable="disable"
          :duration="duration" :prop="size" :rebounce="rebounce"
          :show="shown" :style="{ zIndex: zIndex }" class="tui-slideview__left tui_wxs_left"
          @mousedown="handler.touchstart" @mousemove="handler.touchmove" @mouseup="handler.touchend"
          @touchend="handler.touchend" @touchmove="handler.touchmove" @touchstart="handler.touchstart"
          @transitionend="handler.transitionEnd">
      <slot></slot>
    </view>
    <view :style="{ zIndex: zIndex }" class="tui-slideview__right tui_wxs_right">
      <view v-if="buttons && buttons.length" class="tui-slideview__buttons">
        <view v-for="(item, index) in buttons" :key="index" class="tui-slideview__btn__wrap tui_wxs_btn">
          <view :data-data="item.data" :data-index="index" :data-isclose="autoClose?1:0"
                :style="{ width: width, height: height, padding: padding, borderRadius: radius, background: item.background }"
                class="tui-slideview__btn"
                @tap="handler.hideButton">
            <view v-if="!item.src" :style="{ fontSize: fontSize + 'rpx', color: item.color }">
              {{ item.text }}
            </view>
            <image v-else :src="item.src" :style="{ width: item.width, height: item.height }"/>
          </view>
        </view>
      </view>
    </view>
    <view v-if="shown && showMask" class="tui-slideview__mask" @tap.stop="closeButtonGroup"
          @touchstart.stop.prevent="closeButtonGroup"></view>
  </view>
</template>
<script lang="wxs" module="handler" src="./tui-slide-view.wxs"></script>
<script>
export default {
  name: 'tuiSlideView',
  emits: ['click', 'close', 'open'],
  props: {
    buttons: {
      type: Array,
      default() {
        return [{
          text: '删除',
          color: '#fff',
          src: '', // img地址
          width: '', //img 宽
          height: '', //img 高
          background: (uni && uni.$tui && uni.$tui.color.pink) || '#f74d54'
        }];
      }
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    padding: {
      type: String,
      default: '0 30rpx'
    },
    radius: {
      type: String,
      default: '0'
    },
    fontSize: {
      type: [Number, String],
      default: 32
    },
    disable: {
      type: Boolean,
      default: false
    },
    show: {
      type: Boolean,
      default: false
    },
    duration: {
      type: Number,
      default: 350
    },
    //滑动多少距离菜单展开
    throttle: {
      type: Number,
      default: 30
    },
    showMask: {
      type: Boolean,
      default: false
    },
    //v2.9.0+
    marginTop: {
      type: [Number, String],
      default: 0
    },
    //v2.9.0+
    marginBottom: {
      type: [Number, String],
      default: 0
    },
    //v2.9.2+ 点击按钮是否自动关闭
    autoClose: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    show(val) {
      this.shown = val;
      this.zIndex = this.shown ? 99 : 9;
    }
  },
  data() {
    return {
      size: null,
      rebounce: 0, //回弹距离，暂时废弃
      shown: false,
      zIndex: 9
    };
  },
  mounted() {
    this.$nextTick(() => {
      setTimeout(() => {
        this.shown = this.show;
        this.updateRight();
      }, 50);
    })
  },
  methods: {
    updateRight() {
      // 获取右侧滑动显示区域的宽度
      const query = uni.createSelectorQuery().in(this);
      query
          .select('.tui_wxs_left')
          .boundingClientRect(res => {
            const btnQuery = uni.createSelectorQuery().in(this);
            btnQuery
                .selectAll('.tui_wxs_btn')
                .boundingClientRect(rects => {
                  this.size = {
                    buttons: rects,
                    button: res,
                    show: this.shown,
                    disable: this.disable,
                    throttle: this.throttle,
                    rebounce: this.rebounce,
                    duration: this.duration
                  };
                })
                .exec();
          })
          .exec();
    },
    buttonTapByWxs(data) {
      if (this.autoClose) {
        this.shown = false;
      }
      this.zIndex = 9;
      this.$emit('click', data);
    },
    hideButton() {
      this.shown = false;
      this.zIndex = 9;
      this.$emit('close', {});
    },
    showButton() {
      this.shown = true;
      this.zIndex = 99;
      this.$emit('open', {});
    },

    transitionEnd() {
    },
    closeButtonGroup() {
      this.hideButton();
    }
  }
};
</script>

<style scoped>
.tui-slide-view {
  position: relative;
  overflow: hidden;
}

.tui-slideview__left {
  width: 100%;
  position: relative;
  z-index: 10;
  cursor: pointer;
}

.tui-slideview__right {
  position: absolute;
  left: 100%;
  top: 0;
  height: 100%;
}

.tui-slideview__btn__wrap {
  position: absolute;
  left: 0;
  bottom: 0;
  text-align: center;
  min-width: 136rpx;
  height: 100%;
  white-space: nowrap;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tui-slideview__buttons {
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.tui-slideview__btn {
  display: flex;
  align-items: center;
  justify-content: center;
}

.tui-slideview__mask {
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  position: fixed;
  left: 0;
  top: 0;
  z-index: 50;
}
</style>
